<template>
  <div class="com-container">
    <div class="com-chart" ref="rank_ref" ></div>
  </div>
</template>

<script>
export default {
  name: "",

  data() {
    return {
      echarts: null,
      allData: null,
      echartsInit: null,
      starValue: 0,
      endValue: 9,
      Interval: null
    };
  },

  mounted() {
    this.initChart();
    this.getData();
    window.addEventListener("resize", this.screenAdapter);
    this.screenAdapter();
  },

  destroyed() {
    window.removeEventListener("resize", this.screenAdapter);
    clearInterval(this.Interval);
  },

  methods: {
    initChart() {
      this.echarts = require("echarts");
      this.echartsInit = this.echarts.init(this.$refs.rank_ref,'chalk');
      const initOption = {
        title: {
          text: "|地区销售排行",
          left: 20,
          top: 20
        },
        tooltip: {
          show: true
        },
        grid: {
          top: "40%",
          left: "5%",
          right: "5%",
          bottom: "5%",
          containLabel: true
        },
        xAxis: { type: "category" },
        yAxis: { type: "value" },
        series: [{
          type: "bar"
        }]
      };
      this.echartsInit.setOption(initOption);
    },

    getData() {
      //获取服务器数据，对this.allData进行赋值后，调用this.upData方法更新图表
      this.axios.get("http://127.0.0.1:8888/api/rank").then(response => {
        this.allData = response.data;
        //console.log(this.allData)
        this.allData.sort((a, b) => {
          return b.value - a.value;
        });
        this.upData();
        this.starInterval();
      });
    },

    upData() {
      const colorArr = [
        ["#0BA82C", "#4FF778"],
        ["#2E72BF", "#23E5E5"],
        ["#5052EE", "#AB6EE5"]
      ];
      //处理图表需要的数据
      const xData = this.allData.map(item => {
        return item.name;
      });
      const yData = this.allData.map(item => {
        return item.value;
      });
      const upOption = {
        xAxis: {
          data: xData
        },
        dataZoom: [
          {
            //坐标系里的柱状图平移动画
            show: false,
            type: "slider",
            startValue: this.starValue,
            endValue: this.endValue
          }
        ],
        series:[ {
          data: yData,
          itemStyle: {
            
            color: arg => {
              let typeArr = null;
              //console.log(arg)
              if (arg.data > 300) {
                typeArr = colorArr[0];
              } else if (arg.data > 200) {
                typeArr = colorArr[1];
              } else {
                typeArr = colorArr[2];
              }
              return new this.echarts.graphic.LinearGradient(0, 0, 1, 0, [
                {
                  offset: 0,
                  color: typeArr[0]
                },
                {
                  offset: 1,
                  color: typeArr[1]
                }
              ]);
            }
          }
        }]
      };
      this.echartsInit.setOption(upOption);
      this.echartsInit.on("mouseover", () => {
        clearInterval(this.Interval);
      });
      this.echartsInit.on("mouseout", () => {
        this.starInterval();
      });
    },

    screenAdapter() {
      const  titleFontSize = this.$refs.rank_ref.offsetWidth/100*3.6
      const adapterOption = {
          series:[{
              barWidth : titleFontSize,
              itemStyle:{
                  barBorderRadius: [titleFontSize/2, titleFontSize/2, 0, 0],
              }
          }],
      };
      this.echartsInit.setOption(adapterOption);
      this.echartsInit.resize();
    },

    starInterval() {
      if (this.Interval) {
          clearInterval(this.Interval)
      }
      this.Interval = setInterval(() => {
        this.starValue++;
        this.endValue++;
        if (this.endValue > this.allData.length - 1) {
          this.starValue = 0;
          this.endValue = 9;
        }
        this.upData();
      }, 2000);
    }
  }
};
</script>
